package com.yunyi.integration.service;

import com.yunyi.integration.service.polymorphism.AbsPolymorphismService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.function.Function;

@Component
public class PolymorphismAdapt2 implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    private Map<String, AbsPolymorphismService> map;

    @PostConstruct
    private void init() {
        map = applicationContext.getBeansOfType(AbsPolymorphismService.class)
                .values().stream()
                .collect(Collectors.toMap(AbsPolymorphismService::getName, Function.identity()));
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public AbsPolymorphismService getStrategy(String str) {
        return map.get(str);
    }


}
